It doesn't matter how a matrix was created - using the vector or matrix
methods, there are two ways to access it. You can either specify an
row and column, or you can specify a particular element. For example:
> a = rand(3,4)
a =
matrix columns 1 thru 4
1 0.333 0.665 0.167
0.975 0.0369 0.0847 0.655
0.647 0.162 0.204 0.129
> a[1,4]
0.167
> a[4]
0.333
> v = 1:4
v =
matrix columns 1 thru 4
1 2 3 4
> v[1;3]
3
> v[2]
2
As you can see, matrices are stored internally in a column-wise
fashion. To force a matrix to it's internal form you can use the
`[:]
' operator:
> a = rand(2,3)
a =
matrix columns 1 thru 3
1 0.647 0.0369
0.975 0.333 0.162
> a [ : ]
matrix columns 1 thru 1
1
0.975
0.647
0.333
0.0369
0.162
In addition to accessing single elements, we can also access partial
rows and/or columns of a matrix. We use the `;
' symbol to
delimit row and column indicies, and the `,
' symbol to delimit
individual row or column indicies.
To reference an entire row or column, we leave out the column or
row index respectively:
> g = rand(3,4)
g =
1 0.333 0.665 0.167
0.975 0.0369 0.0847 0.655
0.647 0.162 0.204 0.129
> g[3;]
0.647 0.162 0.204 0.129
> g[;2]
0.333
0.0369
0.162
To reference a sub-matrix, we just specify which rows and columns are
to be extracted:
> g[2; 3,4]
0.0847 0.655
As stated previously, any expression that evaluates to a matrix can
have it's elements referenced. For example, the size()
function
returns a two element matrix, where the first element contains the
number of rows in the argument, and the second element contains the
number of columns. For example:
> size(g)[2]
4